<template>
  <div class="brand-list">
    <v-title :title="$route.meta.title || '品牌列表'" />
    <arrow />
    <loading :show="loading" />
    <ul class="list">
      <li
        v-for="(item, index) in list"
        :key="index"
        class="list-item"
        @click="$router.push(`/product/brand-view/${item.id}`)"
      >
        <div class="image" v-if="item.image && item.image.thumbnail_path">
          <img v-lazy="item.image.thumbnail_path" />
        </div>
        <div class="name">{{ item.name }}</div>
      </li>
    </ul>
    <pull-up
      :loading="loading"
      :busy="queryList.busy"
      @loadMore="loadMore" />
  </div>
</template>

<script>
import Title from '@/components/Title'
import Arrow from '@/components/Arrow'
import Loading from '@/components/Loading'
import api from '@/api'
import base64 from '@/util/base64'
import PullUp from '@/components/PullUp'

export default {
  name: 'BrandList',
  components: {
    'v-title': Title,
    Arrow,
    Loading,
    PullUp
  },
  data() {
    return {
      loading: false,
      list: [],
      queryList: {
        page: 0,
        pageSize: 10,
        busy: false
      }
    }
  },
  methods: {
    loadMore() {
      setTimeout(() => {
        this.getList(true)
      }, 200)
    },
    getList(append = false) {
      if (this.queryList.busy === false) {
        this.queryList.page++
        this.loading = true
        this.queryList.busy = true
        api.Product.brandList({ ...this.queryList }).then(response => {
          let { data } = response
          data = base64.parse(data)
          if (data.list.length === 0) {
            this.queryList.busy = true
          } else {
            this.queryList.busy = false
            if (append) {
              this.list = this.list.concat(data.list)
            } else {
              this.list = data.list
            }
          }
        }).finally(() => {
          this.loading = false
        })
      }
    }
  },
  created () {
    this.getList()
  },
  activated () {
    // this.getList()
  },
  deactivated () {
    this.queryList.busy = true
  }
}
</script>

<style scoped lang="stylus">
.brand-list
  padding-top 45px
  .list
    margin 10px
    display flex
    flex-wrap wrap
    justify-content space-between
    .list-item
      padding 10px
      border-radius 10px
      background white
      margin-bottom 10px
      width calc(50% - 5px)
      box-sizing border-box
      display flex
      flex-direction column
      justify-content space-between
      .image
        padding 10px
      .name
        font-weight 700
        font-size 14px
        line-height 1.3em
</style>
